package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sessions")
public class SessionController {
    @Autowired
    private IUserService userService;

    /**
     * 登录请求
     * @param email
     * @param password
     * @return
     */
    @PostMapping
    public JsonResult login(@RequestParam String email, @RequestParam String password){
        JsonResult result = new JsonResult();
        try {
            User user = userService.login(email,password);
            UserContext.setCurrentUser(user);
            result.setResult(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 注销请求
     * @return
     */
    @DeleteMapping
    public JsonResult logout(){
        UserContext.getSession().invalidate();
        return new JsonResult();
    }
}
